<template>
  <div class="goods-page">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="ipt">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="query"
            clearable
            @clear="clearFn"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodList"
            ></el-button>
          </el-input>
        </div>
        <el-button type="primary" @click="goAddpage">添加商品</el-button>
      </div>
      <el-table border :data="list">
        <el-table-column type="index" width="50" label="序号">
        </el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          width="200"
        ></el-table-column>
        <el-table-column label="商品价格" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="200">
          <template #default="{ row }">
            {{ row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="isDialogFormShow('edit', row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delUserInfo(row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[10, 15, 20, 30]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="changeSize"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { delGoodApi, getGoodListApi } from "@/api/e-commerce";
import { Message } from "element-ui";

export default {
  name: "GoodsIndex",
  data() {
    return {
      query: "",
      pagenum: 1,
      pagesize: 10,
      list: [],
      total: 0,
    };
  },
  created() {
    this.getGoodList();
  },
  methods: {
    goAddpage() {
      this.$router.push("/goods/add");
    },
    // 编辑功能
    isDialogFormShow(type, row) {
      console.log(type, row);
    },
    async delUserInfo(id) {
      try {
        // 必须加await  不然点开框的同时就会提示成功
        await this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
        await delGoodApi(id);
        this.getGoodList();
        Message.success("删除成功");
      } catch (err) {
        console.log(err);
        Message.error("已取消删除");
      }
    },
    clearFn() {
      this.getGoodList();
    },
    async getGoodList() {
      const res = await getGoodListApi({
        query: this.query,
        pagenum: this.pagenum,
        pagesize: this.pagesize,
      });
      console.log(res);
      this.list = res.data.data.goods;
      this.total = res.data.data.total;
    },
    changeSize(value) {
      this.pagesize = value;
      this.getGoodList();
    },
    handleCurrentChange(value) {
      this.pagenum = value;
      this.getGoodList();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
.clearfix {
  display: flex;
  .ipt {
    margin-right: 20px;
  }
}
</style>
